{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "151f2368-58a1-4105-8ba2-6576e1d8f6a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d86d78a2-e131-443e-9c65-252cd62866d5",
   "metadata": {},
   "source": [
    "1.创建多层索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fb147ba7-f3e0-40da-b5fa-966ca9452013",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>30</td>\n",
       "      <td>89</td>\n",
       "      <td>72</td>\n",
       "      <td>46</td>\n",
       "      <td>13</td>\n",
       "      <td>93</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>93</td>\n",
       "      <td>26</td>\n",
       "      <td>91</td>\n",
       "      <td>74</td>\n",
       "      <td>26</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>21</td>\n",
       "      <td>26</td>\n",
       "      <td>92</td>\n",
       "      <td>40</td>\n",
       "      <td>56</td>\n",
       "      <td>56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>鲁班</th>\n",
       "      <td>19</td>\n",
       "      <td>17</td>\n",
       "      <td>19</td>\n",
       "      <td>87</td>\n",
       "      <td>2</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张三丰</th>\n",
       "      <td>39</td>\n",
       "      <td>66</td>\n",
       "      <td>38</td>\n",
       "      <td>52</td>\n",
       "      <td>8</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张无忌</th>\n",
       "      <td>20</td>\n",
       "      <td>29</td>\n",
       "      <td>7</td>\n",
       "      <td>41</td>\n",
       "      <td>64</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        期中          期末        \n",
       "        语文  数学  英语  语文  数学  英语\n",
       "1班 张三   30  89  72  46  13  93\n",
       "   李四   93  26  91  74  26  74\n",
       "   王五   21  26  92  40  56  56\n",
       "2班 鲁班   19  17  19  87   2  27\n",
       "   张三丰  39  66  38  52   8  32\n",
       "   张无忌  20  29   7  41  64  67"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0,100,size = (6,6))\n",
    "index = [\n",
    "    ['1班','1班','1班','2班','2班','2班'],\n",
    "    ['张三','李四','王五','鲁班','张三丰','张无忌']\n",
    "]\n",
    "columns = [\n",
    "    ['期中','期中','期中','期末','期末','期末'],\n",
    "    ['语文','数学','英语','语文','数学','英语']\n",
    "]\n",
    "df = pd.DataFrame(data = data, index = index, columns = columns)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a0612dc1-6736-4972-8762-4e658862e986",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1班  张三     38\n",
       "    李四     57\n",
       "    王五      2\n",
       "2班  鲁班     94\n",
       "    张三丰    63\n",
       "    张无忌    34\n",
       "dtype: int32"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0,100,size = 6)\n",
    "index = [\n",
    "    ['1班','1班','1班','2班','2班','2班'],\n",
    "    ['张三','李四','王五','鲁班','张三丰','张无忌']\n",
    "]\n",
    "s = pd.Series(data = data, index = index)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20649968-89a3-41da-a365-1d39fe4e418a",
   "metadata": {},
   "source": [
    "显示构造pd.MultiIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "767e420b-4211-413c-887a-d7b5c0497151",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>18</td>\n",
       "      <td>40</td>\n",
       "      <td>58</td>\n",
       "      <td>56</td>\n",
       "      <td>25</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>82</td>\n",
       "      <td>16</td>\n",
       "      <td>40</td>\n",
       "      <td>65</td>\n",
       "      <td>68</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>2</td>\n",
       "      <td>65</td>\n",
       "      <td>71</td>\n",
       "      <td>43</td>\n",
       "      <td>49</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>鲁班</th>\n",
       "      <td>40</td>\n",
       "      <td>73</td>\n",
       "      <td>44</td>\n",
       "      <td>10</td>\n",
       "      <td>88</td>\n",
       "      <td>62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张三丰</th>\n",
       "      <td>32</td>\n",
       "      <td>48</td>\n",
       "      <td>38</td>\n",
       "      <td>10</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张无忌</th>\n",
       "      <td>96</td>\n",
       "      <td>67</td>\n",
       "      <td>87</td>\n",
       "      <td>77</td>\n",
       "      <td>71</td>\n",
       "      <td>97</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        期中          期末        \n",
       "        语文  数学  英语  语文  数学  英语\n",
       "1班 张三   18  40  58  56  25  94\n",
       "   李四   82  16  40  65  68  88\n",
       "   王五    2  65  71  43  49  85\n",
       "2班 鲁班   40  73  44  10  88  62\n",
       "   张三丰  32  48  38  10  38  64\n",
       "   张无忌  96  67  87  77  71  97"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0,100,size = (6,6))\n",
    "index = pd.MultiIndex.from_arrays([\n",
    "    ['1班','1班','1班','2班','2班','2班'],\n",
    "    ['张三','李四','王五','鲁班','张三丰','张无忌']\n",
    "])\n",
    "columns = [\n",
    "    ['期中','期中','期中','期末','期末','期末'],\n",
    "    ['语文','数学','英语','语文','数学','英语']\n",
    "]\n",
    "df = pd.DataFrame(data = data, index = index, columns = columns)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47534cee-54f5-40de-bd7f-a5ea314aede5",
   "metadata": {},
   "source": [
    "使用元组tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6ae81e93-5431-4f09-844f-9ea6efc0364b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>88</td>\n",
       "      <td>75</td>\n",
       "      <td>16</td>\n",
       "      <td>57</td>\n",
       "      <td>32</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>85</td>\n",
       "      <td>39</td>\n",
       "      <td>43</td>\n",
       "      <td>97</td>\n",
       "      <td>58</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>0</td>\n",
       "      <td>22</td>\n",
       "      <td>80</td>\n",
       "      <td>31</td>\n",
       "      <td>20</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>鲁班</th>\n",
       "      <td>61</td>\n",
       "      <td>80</td>\n",
       "      <td>39</td>\n",
       "      <td>53</td>\n",
       "      <td>41</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张三丰</th>\n",
       "      <td>68</td>\n",
       "      <td>51</td>\n",
       "      <td>4</td>\n",
       "      <td>85</td>\n",
       "      <td>24</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张无忌</th>\n",
       "      <td>89</td>\n",
       "      <td>68</td>\n",
       "      <td>54</td>\n",
       "      <td>10</td>\n",
       "      <td>51</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        期中          期末        \n",
       "        语文  数学  英语  语文  数学  英语\n",
       "1班 张三   88  75  16  57  32  70\n",
       "   李四   85  39  43  97  58  60\n",
       "   王五    0  22  80  31  20  52\n",
       "2班 鲁班   61  80  39  53  41  99\n",
       "   张三丰  68  51   4  85  24  85\n",
       "   张无忌  89  68  54  10  51  91"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0,100,size = (6,6))\n",
    "index = pd.MultiIndex.from_tuples(\n",
    "    (\n",
    "        ('1班','张三'),('1班','李四'),('1班','王五'),\n",
    "        ('2班','鲁班'),('2班','张三丰'),('2班','张无忌')\n",
    "    )\n",
    ")\n",
    "\n",
    "columns = [\n",
    "    ['期中','期中','期中','期末','期末','期末'],\n",
    "    ['语文','数学','英语','语文','数学','英语']\n",
    "]\n",
    "df = pd.DataFrame(data = data, index = index, columns = columns)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23af495e-ba45-4a79-9106-14a95024c2c2",
   "metadata": {},
   "source": [
    "使用product\n",
    "笛卡尔积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5e654954-80f9-4e14-b807-a673ed098d9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>62</td>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>34</td>\n",
       "      <td>64</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>73</td>\n",
       "      <td>17</td>\n",
       "      <td>84</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>70</td>\n",
       "      <td>0</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>26</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>31</td>\n",
       "      <td>51</td>\n",
       "      <td>99</td>\n",
       "      <td>68</td>\n",
       "      <td>38</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>72</td>\n",
       "      <td>17</td>\n",
       "      <td>43</td>\n",
       "      <td>50</td>\n",
       "      <td>92</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>69</td>\n",
       "      <td>72</td>\n",
       "      <td>38</td>\n",
       "      <td>78</td>\n",
       "      <td>6</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  62  17   4  34  64  68\n",
       "   李四   1   1  73  17  84  91\n",
       "   王五  70   0  54   0  26  28\n",
       "2班 张三  31  51  99  68  38  60\n",
       "   李四  72  17  43  50  92  88\n",
       "   王五  69  72  38  78   6  74"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0,100,size = (6,6))\n",
    "#笛卡尔积:{a,b},{c,d} => {a,c},{a,d},{b,c},{b,d}\n",
    "index = pd.MultiIndex.from_product([\n",
    "    ['1班','2班'],\n",
    "    ['张三','李四','王五']\n",
    "])\n",
    "columns = [\n",
    "    ['期中','期中','期中','期末','期末','期末'],\n",
    "    ['语文','数学','英语','语文','数学','英语']\n",
    "]\n",
    "df = pd.DataFrame(data = data, index = index, columns = columns)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e7b8cef7-ae7a-4ac8-a9d3-b24b8d2fee4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1班  张三     38\n",
       "    李四     57\n",
       "    王五      2\n",
       "2班  鲁班     94\n",
       "    张三丰    63\n",
       "    张无忌    34\n",
       "dtype: int32"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8bb17c56-05dd-4927-9e03-7fed3c0e1119",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Jamal Crawford\\AppData\\Local\\Temp\\ipykernel_18764\\1285396289.py:13: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  s[0]\n",
      "C:\\Users\\Jamal Crawford\\AppData\\Local\\Temp\\ipykernel_18764\\1285396289.py:14: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  s[1]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1班  李四    57\n",
       "    王五     2\n",
       "dtype: int32"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##显式索引\n",
    "s['1班']\n",
    "s.loc['1班']\n",
    "\n",
    "s[['1班']]\n",
    "s[['1班','2班']]\n",
    "s['1班']['张三']\n",
    "s.loc['1班','张三']\n",
    "s['1班','张三']\n",
    "\n",
    "\n",
    "##隐式索引\n",
    "s[0]\n",
    "s[1]\n",
    "s.iloc[[1,2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d856f7d7-5a37-4622-88d1-562391ffb0af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1班  李四     57\n",
       "    王五      2\n",
       "2班  鲁班     94\n",
       "    张三丰    63\n",
       "dtype: int32"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#显式切片\n",
    "s['1班':'2班']\n",
    "s.loc['1班':'2班']\n",
    "s.loc['1班']['李四':'王五']\n",
    "s.loc['1班'][:]\n",
    "#建议使用隐式索引\n",
    "s[1:5]\n",
    "s.iloc[1:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "0faf8706-dd47-4705-98b2-ed721538954e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>62</td>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>34</td>\n",
       "      <td>64</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>73</td>\n",
       "      <td>17</td>\n",
       "      <td>84</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>70</td>\n",
       "      <td>0</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>26</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>31</td>\n",
       "      <td>51</td>\n",
       "      <td>99</td>\n",
       "      <td>68</td>\n",
       "      <td>38</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>72</td>\n",
       "      <td>17</td>\n",
       "      <td>43</td>\n",
       "      <td>50</td>\n",
       "      <td>92</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>69</td>\n",
       "      <td>72</td>\n",
       "      <td>38</td>\n",
       "      <td>78</td>\n",
       "      <td>6</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  62  17   4  34  64  68\n",
       "   李四   1   1  73  17  84  91\n",
       "   王五  70   0  54   0  26  28\n",
       "2班 张三  31  51  99  68  38  60\n",
       "   李四  72  17  43  50  92  88\n",
       "   王五  69  72  38  78   6  74"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "15767067-41b9-4228-86fa-e50526651adc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##获取元素\n",
    "df['期中']['数学']['1班']['张三']\n",
    "df.iloc[0,1]\n",
    "df.loc[('1班','张三'),('期中','数学')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "f47778b6-e895-4d7f-90d5-680852d6484a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>62</td>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>34</td>\n",
       "      <td>64</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>73</td>\n",
       "      <td>17</td>\n",
       "      <td>84</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>70</td>\n",
       "      <td>0</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>26</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>31</td>\n",
       "      <td>51</td>\n",
       "      <td>99</td>\n",
       "      <td>68</td>\n",
       "      <td>38</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>72</td>\n",
       "      <td>17</td>\n",
       "      <td>43</td>\n",
       "      <td>50</td>\n",
       "      <td>92</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>69</td>\n",
       "      <td>72</td>\n",
       "      <td>38</td>\n",
       "      <td>78</td>\n",
       "      <td>6</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  62  17   4  34  64  68\n",
       "   李四   1   1  73  17  84  91\n",
       "   王五  70   0  54   0  26  28\n",
       "2班 张三  31  51  99  68  38  60\n",
       "   李四  72  17  43  50  92  88\n",
       "   王五  69  72  38  78   6  74"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "a254b91d-5bad-411d-b2f1-57f67799124d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1班</th>\n",
       "      <th>李四</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>73</td>\n",
       "      <td>17</td>\n",
       "      <td>84</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>31</td>\n",
       "      <td>51</td>\n",
       "      <td>99</td>\n",
       "      <td>68</td>\n",
       "      <td>38</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>72</td>\n",
       "      <td>17</td>\n",
       "      <td>43</td>\n",
       "      <td>50</td>\n",
       "      <td>92</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1班</th>\n",
       "      <th>王五</th>\n",
       "      <td>70</td>\n",
       "      <td>0</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>26</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 李四   1   1  73  17  84  91\n",
       "2班 张三  31  51  99  68  38  60\n",
       "   李四  72  17  43  50  92  88\n",
       "1班 王五  70   0  54   0  26  28"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取索引\n",
    "#列索引\n",
    "df['期中'][['数学']]\n",
    "df['期中','数学']\n",
    "df.期中.数学\n",
    "df.iloc[:,2]\n",
    "df.iloc[:,[0,2,1]]\n",
    "df.loc[:,('期中','数学')]\n",
    "#行索引\n",
    "df.loc[('1班','张三')]\n",
    "df.loc['1班','张三']\n",
    "df.loc['1班'].loc['张三']\n",
    "df.iloc[1]\n",
    "df.iloc[[1]]\n",
    "df.iloc[[1,3,4,2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "41f77453-0d68-4b7d-9975-25349b1b49b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>62</td>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>34</td>\n",
       "      <td>64</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>73</td>\n",
       "      <td>17</td>\n",
       "      <td>84</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>70</td>\n",
       "      <td>0</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>26</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>31</td>\n",
       "      <td>51</td>\n",
       "      <td>99</td>\n",
       "      <td>68</td>\n",
       "      <td>38</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>72</td>\n",
       "      <td>17</td>\n",
       "      <td>43</td>\n",
       "      <td>50</td>\n",
       "      <td>92</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>69</td>\n",
       "      <td>72</td>\n",
       "      <td>38</td>\n",
       "      <td>78</td>\n",
       "      <td>6</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  62  17   4  34  64  68\n",
       "   李四   1   1  73  17  84  91\n",
       "   王五  70   0  54   0  26  28\n",
       "2班 张三  31  51  99  68  38  60\n",
       "   李四  72  17  43  50  92  88\n",
       "   王五  69  72  38  78   6  74"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "5a263b41-8117-46d1-906d-cf4d36b015ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>62</td>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>34</td>\n",
       "      <td>64</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>73</td>\n",
       "      <td>17</td>\n",
       "      <td>84</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>70</td>\n",
       "      <td>0</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>26</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>31</td>\n",
       "      <td>51</td>\n",
       "      <td>99</td>\n",
       "      <td>68</td>\n",
       "      <td>38</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>72</td>\n",
       "      <td>17</td>\n",
       "      <td>43</td>\n",
       "      <td>50</td>\n",
       "      <td>92</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>69</td>\n",
       "      <td>72</td>\n",
       "      <td>38</td>\n",
       "      <td>78</td>\n",
       "      <td>6</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  62  17   4  34  64  68\n",
       "   李四   1   1  73  17  84  91\n",
       "   王五  70   0  54   0  26  28\n",
       "2班 张三  31  51  99  68  38  60\n",
       "   李四  72  17  43  50  92  88\n",
       "   王五  69  72  38  78   6  74"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#行切片\n",
    "df.iloc[1:5]\n",
    "df.loc[('1班','李四'):('2班','李四')]\n",
    "df.loc['1班':'2班']\n",
    "#列切片\n",
    "df.iloc[:,1:5]\n",
    "df.loc[:,'期中':'期末']\n",
    "#df.loc[:,('期中','数学'):('期末','数学')]#报错\n",
    "#切片建议使用隐式索引:使用数字下标做切片\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70cbeba5-0026-4722-9656-245504ad54d2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
